<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="渠道编号" prop="channelCode">
        <el-input v-model="queryParams.channelCode" placeholder="请输入渠道编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-model="queryParams.channelName" clearable placeholder="请选择渠道名称" filterable>
          <el-option v-for="(item, i) in extData.channelNameList || []" :key="i" :label="item.key"
            :value="item.key"></el-option>
        </el-select>
      </el-form-item>

      
      <el-form-item label="MD5" prop="mobileMd5">
        <el-input v-model="queryParams.mobileMd5" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入应该名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item label="省市区" prop="provincial">
        <el-input v-model="queryParams.provincial" placeholder="请输入省市区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>   


      <el-form-item v-for="item in dynamicParamList" :key="item.filedName" :label="item.name" :prop="item.filedName">
        <el-select multiple style="width: 100%" v-model="form[item.filedName]" :placeholder="`请选择${item.name}`"
          @change="handleQuery" :allow-clear="true" clearable>
          <el-option v-for="itemOp in item.children" :key="itemOp.value" :label="itemOp.name" :value="itemOp.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="年龄" >
        <div class="flex">
          <el-input-number :min="0" v-model="queryParams.minAge"  />
          <div style="margin: 0 8px">至</div>
          <el-input-number :min="0" v-model="queryParams.maxAge"  />
        </div>
      </el-form-item>

      <el-form-item label="注册时间">
        <MobileDatePicker v-model="queryParams.dateRange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-place" size="small" @click="openCity">城市分布图</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="numberTempList">
      <el-table-column label="用户ID" align="center" prop="id" width="170">
        <template slot-scope="scope">
          <span class="clickable-uid" @click="handleDetailsDialog(scope.row)">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道信息" align="center" width="170">
        <template slot-scope="scope" align="center">
          <div>
            <p>[{{ scope.row.channelCode }}] {{ scope.row.channelName }} </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手机号Md5" align="center" prop="mobileMd5" />
      <el-table-column label="应用信息" align="center" width="200">
        <template slot-scope="scope" align="center">
          <div>
            <p>[{{ scope.row.appCode }}] {{ scope.row.appName }} </p>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="个人信息" align="center" width="150">
        <template slot-scope="scope" align="center">
          {{ scope.row.realName }}--{{ scope.row.sex === '1' ? '男' : '女' }}--{{ scope.row.age }}岁
        </template>
      </el-table-column>
   

      <el-table-column label="所属地" align="center" width="200">
        <template slot-scope="scope" align="center">
          <div>
            <p>[{{ scope.row.province }}] {{ scope.row.city }} </p>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="手机号MD5" align="center" prop="tmobile" /> -->
      <!-- <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" /> -->

      <el-table-column label="芝麻分数" align="center" prop="zhima" />
      <el-table-column label="贷款时间 / 金额" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.loanTime }} / {{ scope.row.loanAmount }}
        </template>
      </el-table-column>
      <el-table-column label="房 / 车" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.house }} / {{ scope.row.car }}
        </template>
      </el-table-column>
      <el-table-column label="社保 / 公积金" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.shebao }} /{{ scope.row.gjj }}
        </template>
      </el-table-column>
      <el-table-column label="花呗 / 白条" align="center" width="120">
        <template slot-scope="scope">
          {{ scope.row.huabei }}/ {{ scope.row.baitiao }}
        </template>
      </el-table-column>
      <el-table-column label="商业保险" align="center" prop="baoxian" />
      <el-table-column label="微粒花" align="center" prop="weili" />
      <el-table-column label="月收入" align="center" prop="shouru" />
      <el-table-column label="学历" align="center" prop="xueli" />
      <el-table-column label="贷款用途" align="center" prop="yongtu" />
      <el-table-column label="逾期情况" align="center" prop="yuqi" />
      <el-table-column label="信用卡额度" align="center" prop="xinyongka" />
      <el-table-column label="工作情况" align="center" prop="gongzuo" /> -->
      <el-table-column label="用户IP" align="center" prop="userIp" />
      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          {{ formatTimestamp(scope.row.createTime) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 用户详情弹窗 -->
    <Member ref="MemberRef" />


    <el-dialog :visible="open" @close="open = false" title="城市分布图:" width="50%">
      <City ref="CityRef" />
    </el-dialog>
  </div>
</template>

<script>
import { listNumberTemp, getNumberTemp, delNumberTemp, addNumberTemp, updateNumberTemp } from "@/api/source/numberTemp";
import { listTaskDictInfo } from "@/api/dict/dictInfo";
import { baseInfoExtData } from "@/api/source/baseInfo";
import Member from '@/views/source/memeber.vue';
import dayjs from 'dayjs';
import City from '@/views/source/city/city.vue'
export default {
  name: "NumberTemp",
  components: {
    Member,
    City
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      showDialog: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线索用户资质信息表格数据
      numberTempList: [],
      numberTempLists: [],
      dynamicParamList: [],
      extData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        realName: null,
        realNameMd5: null,
        mobile: null,
        mobileMd5: null,
        idCard: null,
        idCardMd5: null,
        age: null,
        sex: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        userIp: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        shouru: null,
        xueli: null,
        yongtu: null,
        yuqi: null,
        xinyongka: null,
        orFlag: null,
        gongzuo: null,
        startCreateTime: null,
        endCreateTime: null,
        dateRange: [`${dayjs().format('YYYY-MM-DD')} 00:00:00`, `${dayjs().format('YYYY-MM-DD')} 23:59:59`],
      },
      // 表单参数
      form: {
        orFlag: '0'
      },
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        mobileMd5: [
          { required: true, message: "手机号MD5不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getOptions();
  },
  methods: {
    /** 查询线索用户资质信息列表 */
    getList() {
      this.loading = true;
      const sata = { ...this.queryParams }
      if (sata.dateRange && sata.dateRange.length === 2) {
        const startDate = this.queryParams.dateRange[0];
        const endDate = this.queryParams.dateRange[1];
        sata.startCreateTime = startDate;
        sata.endCreateTime = endDate;
      }
      delete sata.dateRange
      listNumberTemp(sata).then(response => {
        this.numberTempList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },

    async handleDetailsDialog(row) {
      this.$refs.MemberRef.show(row.id)
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        realName: null,
        realNameMd5: null,
        mobile: null,
        mobileMd5: null,
        idCard: null,
        idCardMd5: null,
        age: null,
        sex: null,
        city: null,
        cityCode: null,
        province: null,
        provinceCode: null,
        userIp: null,
        loanAmount: null,
        loanTime: null,
        house: null,
        car: null,
        gjj: null,
        shebao: null,
        baoxian: null,
        zhima: null,
        huabei: null,
        baitiao: null,
        weili: null,
        shouru: null,
        xueli: null,
        yongtu: null,
        yuqi: null,
        xinyongka: null,
        gongzuo: null,
        minAge: null,
        maxAge: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      for (const item of this.dynamicParamList) {
        const fieldName = item.filedName;
        const selectedValues = this.form[fieldName];
        if (Array.isArray(selectedValues)) {
          this.queryParams[fieldName] = selectedValues.join(',');
        } else {
          this.queryParams[fieldName] = selectedValues;
        }
      }
      this.getList();
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listTaskDictInfo().then((response) => {
        this.dynamicParamList = this.handleTree(response.rows, "id");
      });
    },


    //    getTreeselect() {
    //   listTaskDictInfo().then((response) => {
    //     // .filter((item) => )
    //     this.dynamicParamList = this.handleTree(response.rows, "id").filter(item => item.name !== '任选');
    //   });
    // },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("form");
      for (const item of this.dynamicParamList) {
        this.form[item.filedName] = null;
      }
      this.queryParams.dateRange = [];
      this.queryParams.uid = null;
      this.queryParams.mobile = null;
      this.queryParams.provincial = null;
      this.queryParams.appName = null;
      this.queryParams.channelName = null;
      this.queryParams.minAge = null;
      this.queryParams.maxAge = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线索用户资质信息";
    },


    openCity() {
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNumberTemp(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线索用户资质信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNumberTemp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNumberTemp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线索用户资质信息编号为"' + ids + '"的数据项？').then(function () {
        return delNumberTemp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('source/numberTemp/export', {
        ...this.queryParams
      }, `numberTemp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.clickable-uid {
  cursor: pointer;
  color: blue;
}
</style>